package org.shj.book1.ioblock;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public abstract class AbstractCalTotal {
	private static String[] stockArr = {
			"600401.SS,100",  "600120.SS,200", "600728.SS,300", "600268.SS,400", "601258.SS,500",
			//"AMGN,100",  "AMZN,200", "BAC,300", "AAPL,400", "HYGF,500",
			//"ZHFJ,100",  "dlkg,200", "BMY,300", "KDXF,400", "CWSZ,500",
			//"FZDJ,100",  "GDNZ,200", "htdl,300", "zsyh,400", "JLQC,500",
			//"JMGF,100",  "PDJT,200", "GLM,300", "ZGQY,400", "hyjx,500",
	};

	public static Map<String, Integer> readTickers() throws IOException{
		Map<String, Integer> stocks = new HashMap<>();
		
		String[] info = null;
		for(int i = 0 ; i < stockArr.length; i++){
			info = stockArr[i].split(",");
			stocks.put(info[0], Integer.valueOf(info[1]));
		}
		return stocks;
	}
	
	public void timeAndComputeValue() throws ExecutionException, InterruptedException, IOException{
		long start = System.nanoTime();
		Map<String, Integer> stocks = readTickers();
		double nav = computeNetAssetValue(stocks);
		long end = System.nanoTime();
		
		String value = new DecimalFormat("$##,##0.00").format(nav);
		System.out.println("Total net asset value: " + value);
		System.out.println("Time (seconds) taken: " + (end - start)/1.0e9);
	}
	
	public abstract double computeNetAssetValue(Map<String, Integer> stocks) throws ExecutionException, InterruptedException, IOException;
}
